/*
 * ContentApplication.java
 *
 * Created on June 25, 2007, 10:36 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.www.util;

import java.util.logging.Level;
import org.atomojo.app.client.Link;
import org.restlet.Application;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.Protocol;
import org.restlet.data.Reference;

/**
 *
 * @author alex
 */
public class ProxyApplication extends Application
{
   String resourceBase;
   String username;
   String password;
   
   static String getStringAttribute(Context context,String name)
   {
      Object obj = context.getAttributes().get(name);
      return obj==null ? null : obj.toString();
   }
   
   /** Creates a new instance of ContentApplication */
   public ProxyApplication(Context context,String resourceBase)
   {
      super(context);
      this.resourceBase = resourceBase;
      this.username = null;
      getTunnelService().setEnabled(false);
   }
   
   /** Creates a new instance of ContentApplication */
   public ProxyApplication(Context context,Link link)
   {
      super(context);
      this.resourceBase = link.getLink().toString();
      this.username = link.getUsername();
      this.password = link.getPassword();
      getTunnelService().setEnabled(false);
   }
   public void setIdentity(String username,String password)
   {
      this.username = username;
      this.password = password;
   }
   
   public Restlet createRoot() {
      return new Restlet(getContext()) {
         boolean isFineLog = getLogger().isLoggable(Level.FINE);
         public void handle(Request request,Response response)
         {
            String path = request.getResourceRef().getRemainingPart();
            if (path==null) {
               path = request.getResourceRef().getPath();
            }
            if (path.length()>0 && path.charAt(0)=='/') {
               path = path.substring(1);
            }
            Reference ref = new Reference(resourceBase+path);
            if (isFineLog) {
               getLogger().fine("Proxy to "+ref);
            }
            Client client = getContext().getClientDispatcher();
            //Client client = new Client(getContext().createChildContext(),Protocol.valueOf(ref.getScheme()));
            client.getContext().getAttributes().put("hostnameVerifier", org.apache.commons.ssl.HostnameVerifier.DEFAULT);
            
            Request appRequest = new Request(request.getMethod(),ref);
            if (request.isEntityAvailable()) {
               appRequest.setEntity(request.getEntity());
            }
            if (username!=null) {
               appRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,username,password));
            }

            Response appResponse = client.handle(appRequest);
            
            response.setStatus(appResponse.getStatus());
            response.setEntity(appResponse.getEntity());
            
         }
      };
   }
   
}
